package cn.dinkevin.xui.util;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.PixelFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.provider.MediaStore;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;

/**
 * 图片处理工具类</br>
 * Created by ChengPengFei on 2016/11/24 0024.</br>
 */

public final class ImageUtil {

    /**
     * 获取调用系统相册选择图片后图片的路径
     * @param context
     * @param data
     * @param resultCode
     * @return 图片路径
     */
    public static String getPicturePathFromActivityResult(Context context,Intent data,int resultCode){
        if (resultCode == Activity.RESULT_OK && data != null)
        {
            Uri selectedImage = data.getData();
            String[] filePathColumns = {MediaStore.Images.Media.DATA};
            Cursor c = context.getContentResolver().query(selectedImage, filePathColumns, null, null, null);
            c.moveToFirst();

            int columnIndex = c.getColumnIndex(filePathColumns[0]);
            String imagePath = c.getString(columnIndex);
            c.close();
            return imagePath;
        }
        return null;
    }

    /**
     * 高斯模糊处理
     * @param context 上下文环境
     * @param src 图片源
     * @param radius 高斯模糊半径
     * @return
     */
    public static Bitmap blur(Context context,Bitmap src,float radius){

        //Let's create an empty bitmap with the same size of the bitmap we want to blur
        Bitmap outBitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);

        //Instantiate a new Renderscript
        RenderScript rs = RenderScript.create(context);

        //Create an Intrinsic Blur Script using the Renderscript
        ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

        //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
        Allocation allIn = Allocation.createFromBitmap(rs, src);
        Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);

        //Set the radius of the blur
        blurScript.setRadius(radius);

        //Perform the Renderscript
        blurScript.setInput(allIn);
        blurScript.forEach(allOut);

        //Copy the final bitmap created by the out Allocation to the outBitmap
        allOut.copyTo(outBitmap);

        //recycle the original bitmap
//        src.recycle();

        //After finishing everything, we destroy the Renderscript.
        rs.destroy();
        return outBitmap;
    }

    /**
     * Drawable 转换到 Bitmap
     * @param drawable
     * @return
     */
    public static Bitmap parseTo(Drawable drawable){
        Bitmap outBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(outBitmap);
        drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return outBitmap;
    }

    /**
     * Bitmap 转换到 Drawable
     * @param context
     * @param src
     * @return
     */
    public static Drawable parseTo(Context context,Bitmap src){
        BitmapDrawable drawable = new BitmapDrawable(context.getResources(),src);
        return drawable;
    }
}
